package operation;

import Dao.MilkDao;
import Dao.OrderDao;
import Dao.VipDao;
import interfac.OrderOperation;
import pojo.MilkTeaProduct;
import pojo.Vip;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AddOrdersOperation implements OrderOperation {
    @Override
    public void execute(Scanner scanner) {
            System.out.println("请输入是否为店铺会员（y/n）");
            String vip = scanner.next();
            if (vip.equals("y")) {
                System.out.println("请输入会员名");
                String vip_name = scanner.next();
                Vip loggedInVip = VipDao.viplogin(vip_name);
                if (loggedInVip != null) {
                    System.out.println("是会员！");
                    add_viporder();
                } else {
                    System.out.println("用户名错误，没有此vip用户");
                    execute(scanner);
                }
            } else if (vip.equals("n")) {
                System.out.println("是否需要注册会员（y/n）");
                String register_vip = scanner.next().trim();
                if (register_vip.equals("y")) {
                    add_vip();
                } else if (register_vip.equals("n")) {
                    add_order();
                } else {
                    System.out.println("请输入正确选择");
                    execute(scanner);
                }

            } else {
                System.out.println("请输入正确选择");
                execute(scanner);
            }
        }

        private static void add_vip () {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String vip_name = scanner.next();
            Vip vip = new Vip(0, vip_name);
            VipDao vipDao = new VipDao();
            vipDao.addVip(vip);
            System.out.println("添加成功");
            add_viporder();
        }

        private static void add_order () {
            Scanner scanner = new Scanner(System.in);
            MilkDao milk_dao = new MilkDao();
            show_milk_tea();
            List<MilkTeaProduct> selectedProducts = new ArrayList<>();
            List<Integer> selectedQuantities = new ArrayList<>();
            boolean continueOrdering = true;
            while (continueOrdering) {
                System.out.println("请输入要选择的奶茶产品ID（输入 -1 结束选择）：");
                int productId = scanner.nextInt();
                if (productId == -1) {
                    scanner.nextLine();
                    break;
                }
                MilkTeaProduct selectedProduct = milk_dao.getProductById(productId);
                if (selectedProduct != null) {
                    System.out.println("请输入选择的 " + selectedProduct.getProduct_id() + " 的数量：");
                    int quantity = scanner.nextInt();
                    if (quantity <= selectedProduct.getStock()) {
                        if (quantity < 0) {
                            System.out.println("数量不能小于0");
                        } else {
                            selectedProducts.add(selectedProduct);
                            selectedQuantities.add(quantity);
                        }
                    } else {
                        System.out.println("库存不足，当前库存为：" + selectedProduct.getStock());
                    }
                } else {
                    System.out.println("输入的产品ID不存在，请重新输入。");
                }
            }

            // 生成订单
            if (selectedProducts.size() > 0) {
                OrderDao orderDao = new OrderDao();
                int[] quantitiesArray = selectedQuantities.stream().mapToInt(Integer::intValue).toArray();
                int orderId = orderDao.generateOrder(selectedProducts, quantitiesArray);
                if (orderId > 0) {
                    System.out.println("点单成功，订单编号为：" + orderId);
                } else {
                    System.out.println("点单操作出现问题，请稍后重试。");
                }
            } else {
                System.out.println("你未选择任何奶茶产品，点单已取消。");
            }
        }

        private static void add_viporder () {
            Scanner scanner = new Scanner(System.in);
            MilkDao milk_dao = new MilkDao();
            show_milk_tea();
            List<MilkTeaProduct> selectedProducts = new ArrayList<>();
            List<Integer> selectedQuantities = new ArrayList<>();
            boolean continueOrdering = true;
            while (continueOrdering) {
                System.out.println("请输入要选择的奶茶产品ID（输入 -1 结束选择）：");
                int productId = scanner.nextInt();
                if (productId == -1) {
                    continueOrdering = false;
                    break;
                }
                MilkTeaProduct selectedProduct = milk_dao.getProductById(productId);
                if (selectedProduct != null) {
                    System.out.println("请输入选择的 " + selectedProduct.getProduct_id() + " 的数量：");
                    int quantity = scanner.nextInt();
                    if (quantity <= selectedProduct.getStock()) {
                        if (quantity < 0) {
                            System.out.println("数量不能小于0");
                        } else {
                            selectedProducts.add(selectedProduct);
                            selectedQuantities.add(quantity);
                        }
                    } else {
                        System.out.println("库存不足，当前库存为：" + selectedProduct.getStock());
                    }
                } else {
                    System.out.println("输入的产品ID不存在，请重新输入。");
                }
            }


            // 生成订单
            if (selectedProducts.size() > 0) {
                OrderDao orderDao = new OrderDao();
                int[] quantitiesArray = selectedQuantities.stream().mapToInt(Integer::intValue).toArray();
                int orderId = orderDao.generateVipOrder(selectedProducts, quantitiesArray);
                if (orderId > 0) {
                    System.out.println("点单成功，订单编号为：" + orderId);
                } else {
                    System.out.println("点单操作出现问题，请稍后重试。");
                }
            } else {
                System.out.println("你未选择任何奶茶产品，点单已取消。");
            }
        }

    private static void show_milk_tea() {
        MilkDao milk_dao = new MilkDao();
        List<MilkTeaProduct> allProducts = milk_dao.getProducts();
        System.out.println("以下是店里的奶茶产品：");
        for (MilkTeaProduct product : allProducts) {
            System.out.println(product.getProduct_id() + " - " + product.getProduct_name() + "，价格：" + product.getPrice() + "，库存：" + product.getStock());
        }
    }
    }
